#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    printf("I am a process,pid:%d\n",getpid());
    pid_t id = fork();
    if(id == 0)
    {
        char *argv[] = {(char*)"ls",(char*)"-a",(char*)"-l"};
        printf("exec begin...\n");
        execv("ls",argv);
        //execv("/usr/bin/ls",argv);
        //execl("/usr/bin/ls","ls","-a","-l",NULL);
        //execlp("ls","ls","-a","-l",NULL);
        
        printf("exec end ....\n");
        exit(-1);
    }
    pid_t rid = waitpid(id,NULL,0);
    if(rid > 0)
    {
        printf("wait success\n");
    }

    return 0;
}

/*
int main()
{
    printf("I am a process,pid:%d\n",getpid());
    printf("/usr/bin/ls","ls");    
    execl("/usr/bin/ls","ls","-a","-l",NULL);
    printf("exec end ....\n");

    return 0;
}

*/


/*
int main()
{
    pid_t id = fork();
    if(id == 0)
    {
        int cnt = 5;
        while(cnt--)
        {
            printf("I am child process,pid:%d,ppid:%d,cnt:%d \n",getpid(),getppid(),cnt);
           sleep(1);
        }
        exit(1);
    }
    
    int status = 0;

    while(1)
    {
        pid_t rid = waitpid(id,&status,WNOHANG);
        if(rid > 0)
        {

            printf("wait success,rid:%d,status:%d,exit signo:%d exit code:%d \n",rid,status,status & 0x7f, (status>>8)&0xff);
            break;
        }
        else if(rid == 0)
        {
            printf("father say:child is runing,do other thing\n");
        }
        else 
        {
            perror("waitpid");
            break;
        }
        sleep(1);
    }


    
    int status = 0;
    pid_t rid = waitpid(id,&status,0);
    if(rid > 0)
    {
        printf("wait success,rid:%d,status:%d,exit signo:%d exit code:%d \n",rid,status,status & 0x7f, (status>>8)&0xff);
    }

    return 0;
}

*/ 


